home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10850 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: void ** question
  5. Date: 20 Mar 1996 09:24:55 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4ioit7$g6k@sparcserver.lrz-muenchen.de>
  9. References: <DoJBrL.F5t@bear.wn.bawue.de>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. jo@bear.wn.bawue.de (Joerg Sommrey) writes:
  13.  
  14. >Hi all,
  15.  
  16. >I have a question concerning conversion to `void **'. Given this piece
  17. >of code:
  18.  
  19. >void f(void **);
  20. >int main(void)
  21. >{
  22. >   int **ppi;
  23. >   f(ppi);
  24. >   return 0;
  25. >}
  26.  
  27. >On four platforms I get three results: silence, warning and error when
  28. >calling f().
  29.  
  30. >The FAQ states there isn't a portable way of calling a function with a
  31. >generic pointer by reference. This seems to be the point but I do not
  32. >understand why.
  33.  
  34. Pointers may have different representations, depending on the type
  35. they point to. They need not even have the same sizes. (An exception
  36. are "void *" and "char *". They must have the same representation
  37. in C. Since every pointer can be converted to a "void *" and back
  38. without loss of information, a "void *" must be able to store all
  39. other pointer types. Yet, a pointer to a "void *" is just a type,
  40. there is nothing generic about it.
  41.  
  42.  
  43. >My questions:
  44. >1) Is `(void **) ppi' legal (which makes any tested compiler shut up)?
  45.  
  46. It tells the compiler that you know what you are doing. If you
  47. _know_ that all pointers have the same representation on your
  48. machine anyway, it may be possible that you know what you are
  49. doing, esp. if the program is not portable anyway.
  50.  
  51. >If it is legal:
  52. >2) Why doesn't this conversion take place silently when calling f()?
  53.  
  54. Because on a lot of machines this will not work, and because the
  55. language is not defined that way. If your function takes a "void *"
  56. as it's argument, you can pass pointers to any type, including
  57. pointers to any pointer type to that function. So what could you
  58. possibly gain from a generic pointer to pointer type?
  59.  
  60. Kurt
  61. --
  62. | Kurt Watzka                             Phone : +49-89-2180-6254
  63. | watzka@stat.uni-muenchen.de
  64. | ua302aa@sunmail.lrz-muenchen.de
  65.